package com.kyd.push.mq.listener;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.kyd.push.model.Order;
import com.kyd.push.model.OrderMqContext;
import com.kyd.push.model.PushTypeEnum;
import com.kyd.push.mq.vo.OrderPushVO;
import com.kyd.push.push.JPushKit;
import com.kyd.push.push.PushRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 订单推送
 *
 * @author kejw on 2018/9/27
 */
@Slf4j
@Component("kyd_order")
public class OrderPushMessageListener implements MessageListener {

	@Override
	public Action consume(Message message, ConsumeContext context) {
		try {
			String body = new String(message.getBody());
			OrderMqContext mqContext = JSON.parseObject(body, OrderMqContext.class);
			Order o = mqContext.getOrder();
			


				OrderPushVO orderPushVO = new OrderPushVO();
				orderPushVO.setOrderId(o.getId());


				String voiceMsg ="";
				String notification = "";
				PushRequest request = PushRequest.newInstance().setNotification(notification).addExtra(PushTypeEnum.ORDER, orderPushVO).addExtra("voiceMsg", voiceMsg);

				JPushKit.sendPushByAlias(request, "");
				// 添加消息记录

			log.error("[MQ]消费失败, errorMsg:{}");


			return Action.CommitMessage;
		} catch (Exception e) {
			//消费失败
			log.error("[MQ]消费失败, errorMsg:{}", e.getMessage());
			return Action.CommitMessage;
		}
	}
}
